<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never" style="border:0px;">
      <div slot="header" style="font-size:14px;font-weight:bold;">
        <span>退件库</span>
      </div>
        <!-- 查询和其他操作 -->
      <div class="filter-container" id="searchBox">
        <div class="left">
          <el-form :inline="true" :model="listQuery" class="demo-form-inline" size="medium" label-position="right" ref="listQuery" >
            <el-form-item label="姓名：" prop="realName">
              <el-input v-model="listQuery.realName" clearable class="filter-item" placeholder="请输入借款人姓名" />
            </el-form-item>
            <el-form-item label="手机号：" prop="mobile">
              <el-input v-model="listQuery.mobile" clearable class="filter-item" placeholder="请输入借款人手机号" />
            </el-form-item>
            <el-form-item label="身份证号：" prop="idCardNo">
              <el-input v-model="listQuery.idCardNo" clearable class="filter-item" placeholder="请输入借款人身份证号" />
            </el-form-item>

            <el-form-item label="拒件时间：" prop="rejectTaskDate">
              <el-date-picker
                class="date_item"
                v-model="listQuery.rejectTaskDate"
                type="daterange"
                range-separator="至"
                align="right"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="贷款类型" prop="loanType">
              <el-select v-model="listQuery.loanType" placeholder="请选择" style="min-width:100%;" @change="getLoanType">
                <el-option
                  v-for="item in loanTypes"
                  :key="item.dictValue"
                  :label="item.dictName"
                  :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目状态" prop="projectStatus">
              <el-select v-model="listQuery.projectStatus" placeholder="请选择" style="min-width:100%;" @change="getList">
                <el-option
                  v-for="item in projectStatusArr"
                  :key="item.dictValue"
                  :label="item.dictName"
                  :value="item.dictValue">
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <div class="right">
             <el-button  type="primary" @click="handleFilter">查询</el-button>
             <el-button  @click="restForm('listQuery')">重置</el-button>
             <el-button  type="text" @click="closeSearch">{{showOperation}}<i :class="[!showAll? 'el-icon-caret-bottom' : 'el-icon-caret-top']"></i>
               </el-button>
        </div>
      </div>

    <vxe-toolbar ref="xToolbar"></vxe-toolbar>
      <vxe-table
      v-loading="loading"
      :data="data"
      border="none"
      highlight-hover-row
      show-header-overflow
      show-overflow
      align="center"
      :header-cell-style="{background:'#F7F7F8',color:'#606266'}"
        @cell-dblclick="cellDblclick"
      >
      <vxe-table-column type="seq" title="序号" min-width="60"/>
      <vxe-table-column title="项目id" field="projectId"  min-width="150"/>
      <vxe-table-column title="客户姓名" field="realName"  min-width="150"/>
      <vxe-table-column title="身份证号" field="idCardNo" min-width="180" />
      <vxe-table-column title="手机号" field="mobile"  min-width="130"/>
      <vxe-table-column title="资方" field="capitalName" min-width="120"/>
      <vxe-table-column title="产品名称" field="capitalSourceName" min-width="120"/>
      <vxe-table-column title="贷款类型" field="loanTypeName" min-width="185"/>
      <vxe-table-column title="借贷金额(万)" field="applyAmount" min-width="140">
        <template v-slot="{row}">{{row.applyAmount}}</template>
      </vxe-table-column>
      <vxe-table-column title="退件时间" field="rejectTime" min-width="180"/>
      <vxe-table-column title="客户经理" field="customerManageName" min-width="150"/>
      <vxe-table-column title="项目状态" field="projectStatusName" min-width="150"/>
      <vxe-table-column title="操作" align="center" fixed="right" min-width="250">
        <template v-slot="{ row }">
          <el-button  type="text" v-if="row.projectStatus==103" @click="secondSubmit(row)" v-loading="loading">再次提交</el-button>
          <el-divider v-if="row.projectStatus==103" direction="vertical"></el-divider>
          <!-- <el-button type="success" size="small" round @click="secondSubmit(row)" v-if="row.projectStatus === 102 && row.parentProjectId === undefined" v-loading="loading">重新提交</el-button> -->
          <el-button  type="text" @click="lookDetail(row)">查看</el-button>
          <el-divider direction="vertical"></el-divider>
          <el-button type="text" v-permission="'system:project-back-menu:transfer'"   @click="setVisbile(row)">归属转移</el-button>
        </template>
      </vxe-table-column>
    </vxe-table>

 </el-card>

    <vxe-pager
      class="pager"
      @page-change="changePage"
      :current-page.sync="listQuery.pageNo"
      :page-size.sync="listQuery.pageSize"
      :total="total"
      :page-sizes="[10, 30, 50,100]"
      :layouts="['PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'Sizes', 'FullJump', 'Total']"
    >
    </vxe-pager>
    <transfer :dialogVisible="dialogVisible" :row="projectRow" @close="transferClose" :projectId="projectId" @getList="getList"/>

  </div>
</template>

<script>
import { backList,startTask,deleteByProjectId,returnStepAdd,backProject } from '@/api/project'
import { secondSubmit } from '@/api/creditLoan'
import DicSelect from '@/components/select'
import Transfer from '@/components/transfer'
import { mapGetters } from 'vuex'
import {getProcessKey} from '@/utils/index'
import { findByDictTypeName } from '@/api/dict'
export default {
  name: 'Index',
  components: { DicSelect,Transfer },
  data() {
    return {
      active: 0,
      data: [],
      total: 0,
      isCustomerManager: false,
      listQuery: {
        pageNo: 1,
        pageSize: 10,
        realNameOrMobile: undefined,
        realName: undefined,
        mobile: undefined,
        idCardNo: undefined,
        pledgeType: undefined,
        submitStartDate: undefined,
        rejectTaskDate: undefined,
        loanType: undefined,
        submitEndDate: undefined,
      },
      loanTypes:undefined,
      projectStatusArr:undefined,
      showAll:true,
      loading: true,
      dialogVisible:false,
      projectRow:{
        realName:undefined,
        idCardNo:undefined,
        transferObj:undefined
      },
      projectId:undefined
    };
  },
  computed: {
    ...mapGetters(['roles','processKey']),
    showOperation: function() {
   if (this.showAll == false) {
      //对文字进行处理
      return "展开";
     } else {
      return "收起";
     }
    }
  },
  mounted(){
  /**
   * 收起搜索
   */
  this.$nextTick(function() {
  //  this.closeSearch();
  });
  },
  created() {
    localStorage.removeItem("projectIdByDraw")
    localStorage.removeItem("borrowerId")
    localStorage.removeItem("projectId")
    localStorage.removeItem("backIdCardNo")
    this.getList();
    if( this.roles.indexOf("客户经理")!=-1){
        this.isCustomerManager = true
    }
    this.findByDictTypeName('项目贷款类型');
  },
  methods: {
    closeSearch() {
       this.showAll = !this.showAll;
       var searchBoxHeght = document.getElementById("searchBox");
       if (this.showAll == false) {
        searchBoxHeght.style.height = 50 + "px";
       } else {
        searchBoxHeght.style.height = "auto";
       }
    },

    findByDictTypeName(dictTypeName) {
      findByDictTypeName(dictTypeName).then((data) => {
        this.loanTypes = data.data;
      });
    },

    // 按照贷款类型查询不同的项目状态
    getLoanType(val) {
      var dictTypeName = null;
      if (val == 1){
        dictTypeName = '项目状态';
      }
      if (val == 2){
        dictTypeName = '信用贷项目状态';
      }
      this.listQuery.projectStatus = undefined;
      findByDictTypeName(dictTypeName).then((data) => {
        this.projectStatusArr = data.data;
      });
      this.getList();
      return false;
    },

    restForm(formName){
      this.$refs[formName].resetFields();
      this.listQuery.rejectTaskDate = undefined;
      this.projectStatusArr = undefined;
      this.getList()
    },
    getList() {
      this.loading = true;
      backList(this.listQuery)
        .then((data) => {
          this.data = data.data;
          this.total = data.total;
          console.log('响应数据:%o', data);
        })
        .catch((error) => {
          console.error('请求用户数据发生异常:' + error);
        });
        this.loading = false;
    },
    handleFilter() {
      this.listQuery.pageNo = 1
      this.getList()
    },
    handelDeleteChoose(row){
      if( row.projectStatus === 3|| row.projectStatus === 0 ){
        // if( (row.projectStatus === 102 || row.projectStatus === 104) && this.isCustomerManager){
        //   return false
        // }
        return true
      }
    },
    handelEditChoose(row){
      if( row.projectStatus === 3  || row.projectStatus === 0 ){
        // if( (row.projectStatus === 102 || row.projectStatus === 104) && this.isCustomerManager){
        //   return false row.projectStatus === 102 || row.projectStatus === 104 ||
        // }
        return true
      }
    },
    deleteByProjectId(row) {
       this.$confirm('确定要删除此笔项目吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           deleteByProjectId(row.id)
            .then((data) => {
              this.$notify.success("删除成功")
              this.getList()
            })
            .catch((err) => {
              console.error('删除失败');
            });
        }).catch(() => {
           this.$message("已取消删除")
        });
     },
    updateUserById(row) {
      this.$XModal.confirm(`确定更新吗${row.name}吗`).then((type) => {
        if (type === 'confirm') {
        }
      });
    },
    resetEvent() {
      this.getList();
    },
    changePage() {
      this.getList();
    },

    startTask(row) {
      this.$confirm('确定提交此笔进件项目吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true;
          startTask(row.id)
              .then((data) => {
              this.$notify({title: '成功',message: '提交成功',type: 'success'});
              this.getList()
              this.loading = false;
            }) .catch(() => {
            this.loading = false;
          })
        }).catch(() => {
            this.loading = false;
           this.$message("已取消提交")
        });
    },

    // 房抵贷重新提交方法
    backProject(row){
      let processKey = getProcessKey(row.processKey)
      backProject(row.projectId).then((data) => {
        this.loading = false
        if(processKey == "dg") {
          this.$router.push({path:"/project/add_dg",query:{submitProjectId:data.data.id,taskId:row.taskId}})
        }else{
          this.$router.push({path:"/project/add",query:{submitProjectId:data.data.id,taskId:row.taskId}})
        }
      }).catch((error) => {
        this.loading = false;
      });
    },

    // 信用贷项目备份
    creditLoanSecondSubmit(row){
      secondSubmit(row.projectId).then((res) => {
        this.loading = false
        this.$router.push({path:"/project/addCredit",query:{projectId: res.data.id}})
      }).catch((error) => {
        this.loading = false;
      });
    },

    secondSubmit(row) {
      this.$confirm('确定开始吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true;
          if (row.loanType == 1){
             this.backProject(row);
          }
          if (row.loanType == 2){
            this.creditLoanSecondSubmit(row)
          }
        }).catch((error) => {
            console.log(error)
            this.loading = false;
           this.$message("已取消提交")
        });
    },
    editUser(row) {
      // let processKey = row.processKey
      let processKey = getProcessKey(row.processKey)
      if(row.projectStatus === 0 ) {
        if(processKey == "dg") {
          this.$router.push({path:"/project/add_dg",query:{ idCardNo: row.idCardNo,projectIdByDraw: row.id,operation: 'edit'}})
        }else{
        this.$router.push({path:"/project/add",query:{ idCardNo: row.idCardNo,projectIdByDraw: row.id,operation: 'edit'}})
        }

      }else{
         //跳转到编辑页面
         if(processKey == "dg") {
          this.$router.push({ path: '/project/edit_dg' , query: { projectId: row.id }})
        }else{
          this.$router.push({ path: '/project/edit' , query: { projectId: row.id }})
        }

      }
    },
    lookDetail(row) {
      //跳转到审批页面
      if (row.loanType == 1){
        let processKey = getProcessKey(row.processKey)
        if(processKey == "dg") {
          this.$router.push({ path: '/detail/deal_dg' , query: { projectId: row.projectId,projectStatus: row.projectStatus,operationLook: true,activeIndex: "1",realName:row.realName }})
        }else{
          this.$router.push({ path: '/detail/deal' , query: { projectId: row.projectId,projectStatus: row.projectStatus,operationLook: true,activeIndex: "1",realName:row.realName }})
        }
      }
      /*信用贷详情页面跳转*/
      if (row.loanType == 2){
        this.$router.push({ path: '/task/deal_credit',
          query: { projectId: row.projectId, taskId: row.id, projectStatus: row.projectStatus,taskStatus: row.taskStatus,realName:row.realName,activeIndex: "1"},
        })
      }
    },
    addUserEvent() {
      this.$router.push({ path: '/project/add',query: {operation: 'add'} })
    },
    evHouseEvent() {
      this.$router.push({ path: '/project/ev' })
    },
    closeModal() {
      this.modalUpdate.row = {};
      this.modalUpdate.originRow = {};
    },
    cellDblclick({row,rowIndex,column,columnIndex,cell},event) {
      let _this = this;
      this.$copyText(event.target.innerText).then(function() {
        _this.$message.info('复制成功');
      });
    },
    setVisbile(row) {
      this.dialogVisible = true
      this.projectId = row.id
      this.projectRow = {
        realName:row.realName,
        idCardNo: row.idCardNo,
        transferObj:undefined
      }
    },
    transferClose() {
      this.dialogVisible =false
    }


  },
};
</script>

<style lang="css" scoped>
.filter-container{
  display: flex;
  justify-content: space-between;

}
#searchBox {
 overflow: hidden;
}

.pager{
  position: fixed;
  bottom: 0;
  right: 28px;
  background-color: #FAFAFB;
  z-index: 2;
  height: 40px;
  width: calc(100% - 260px);
  }

.right >>> .el-button--primary,.el-button--default {
  padding: 10px 20px;
}

.vxe-table--render-default >>> .el-button--text{
  color: #2b7bfb;
}
.app-container >>> .vxe-table--render-default,.vxe-pager {
  font-family: PingFangSC, Arial, Helvetica, sans-serif;
}


.left {
    flex: 14;
    display: flex;
    flex-flow: row wrap;
    margin-right: 20px;


}
.left >>> .el-form{
      width: 100%;
}
.left >>> .el-form .el-form-item {
        display: flex;
        flex-flow: row nowrap;
        width: 30%;
        margin-right: 3%;
        float: left;
        margin-bottom: 14px;
}


.left >>> .el-form .el-form-item .el-form-item__label{
    font-weight: 500;
    color: #606266;
    text-align: left;
     /* flex: 1; */
}

.left >>> .el-form .el-form-item .el-form-item__content {
          flex: 1;
          /*超出部分隐藏*/
          text-overflow: ellipsis;
          /* 超出部分显示省略号 */
          white-space: nowrap;
          /*规定段落中的文本不进行换行 */
          width: 100%;
        }
 .date_item {
   width: 100%;
 }
 .right {
    flex: 2;
    display: flex;
    flex-flow: row nowrap;
    align-items: flex-end;
    margin-right: 30px;
    /* margin-bottom: 30px; */
 }
</style>
